package com.es.spring.aop.interceptor;

import com.es.spring.aop.common.TargetMethod;
import lombok.Data;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 二十
 * @since 2021/11/9 10:12 下午
 */
public interface MyInvocation {

    Object proceed();
}
@Data
class MyInvocationHandler implements MyInvocation{

    public MyInvocationHandler(List<MyInterceptor> interceptors, TargetMethod targetMethod) {
        this.interceptors = interceptors;
        this.targetMethod = targetMethod;
    }

    List<MyInterceptor> interceptors =new ArrayList<>();
    int size =0;
    TargetMethod targetMethod;

    @Override
    public Object proceed() {

        try {
            return interceptors.size()==size?targetMethod.getMethod().invoke(targetMethod.getTarget(),targetMethod.getArgs()):interceptors.get(size++).invoke(this);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }
}
